# Author: HZT
# Date: 2023/9/5
import unittest

from config.config_loader import config
from page.web_sk_hl import WebSkHl
from page.web_sk_jk import WebSkJk
from utils import UtilsDriver, read_json, login_with_session, GetLog
from parameterized import parameterized
import time


class TestWebSkHl(unittest.TestCase):
    skHl = None
    skJk = None
    driver = None
    credentials = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.log = GetLog.get_log()
        cls.driver = UtilsDriver.get_web_driver()
        # 实例化
        cls.skHl = WebSkHl(cls.driver)
        cls.skJk = WebSkJk(cls.driver)
        # 打开网页
        cls.driver.get(config.URL)
        login_with_session('system2', cls.driver)
        # cls.skJk.sk_jk_click_skzx()
        # time.sleep(0.5)
        cls.skHl.sk_hl_click_hlgl()
        elements = cls.skHl.sk_hl_get_kzlb()
        for element in elements:
            if element.text == "t-Steam":
                cls.skHl.sk_hl_click_sc()
                time.sleep(1)
                cls.skHl.sk_hl_click_scqd()
                time.sleep(1)
                cls.skHl.sk_hl_click_bcok()
        cls.skHl.sk_hl_xz()
        time.sleep(1)

    @classmethod
    def tearDownClass(cls):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 新增固定面值成功
    def test01_sk_hl(self):
        try:
            self.skHl.sk_hl_click_je1()
            time.sleep(2)
            self.skHl.sk_hl_click_gdmz()
            self.skHl.sk_hl_input_fw1("100")
            self.skHl.sk_hl_input_hl1("8.8")
            self.skHl.sk_hl_click_bc()
            text = self.skHl.sk_hl_get_bccg()
            self.assertEqual(text, "保存成功")
            time.sleep(1)
            self.skHl.sk_hl_click_bcok()
            time.sleep(1)
        except Exception as e:
            self.log.error("新增固定面值失败,原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 新增固定面值失败
    @parameterized.expand(read_json("sk_hl.json"))
    def test02_sk_hl(self, fw, hl, expect):
        try:
            time.sleep(1)
            self.skHl.sk_hl_input_fw1(fw)
            time.sleep(1)
            self.skHl.sk_hl_input_hl1(hl)
            time.sleep(1)
            self.skHl.sk_hl_click_bc()
            time.sleep(1)
            text = self.skHl.sk_hl_get_bcts()
            self.skHl.sk_hl_click_tsok()
            self.assertIn(expect, text)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 新增面值区间成功
    def test03_sk_hl(self):
        try:
            self.skHl.sk_hl_input_fw1("100")
            time.sleep(1)
            self.skHl.sk_hl_click_tj()
            time.sleep(1)
            self.skHl.sk_hl_click_je2()
            time.sleep(1)
            self.skHl.sk_hl_click_mzqj()
            time.sleep(1)
            self.skHl.sk_hl_click_qjxz()
            time.sleep(1)
            self.skHl.sk_hl_click_xzqj()
            self.skHl.sk_hl_input_hl2("6.7")
            self.skHl.sk_hl_click_bc()
            text = self.skHl.sk_hl_get_bccg()
            self.assertEqual(text, "保存成功")
            time.sleep(1)
            self.skHl.sk_hl_click_bcok()
            time.sleep(1)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 新增碎卡成功
    def test04_sk_hl(self):
        try:
            self.skHl.sk_hl_click_tj()
            time.sleep(1)
            self.skHl.sk_hl_click_je3()
            time.sleep(1)
            self.skHl.sk_hl_click_sk()
            time.sleep(1)
            self.skHl.sk_hl_input_hl3("6.8")
            self.skHl.sk_hl_click_bc()
            text = self.skHl.sk_hl_get_bccg()
            self.assertEqual(text, "保存成功")
            time.sleep(1)
            self.skHl.sk_hl_click_bcok()
            time.sleep(1)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 新增倍数成功
    def test05_sk_hl(self):
        try:
            self.skHl.sk_hl_click_tj()
            time.sleep(1)
            self.skHl.sk_hl_click_je4()
            time.sleep(1)
            self.skHl.sk_hl_click_bs()
            time.sleep(1)
            self.skHl.sk_hl_input_fw4("10")
            self.skHl.sk_hl_input_hl4("6.9")
            self.skHl.sk_hl_click_bc()
            text = self.skHl.sk_hl_get_bccg()
            self.assertEqual(text, "保存成功")
            time.sleep(1)
            self.skHl.sk_hl_click_bcok()
            time.sleep(1)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 修改固定面值失败
    def test06_sk_hl(self):
        try:
            self.skHl.sk_hl_get_srs()[3].clear()
            self.skHl.sk_hl_get_srs()[3].send_keys("-100")
            self.skHl.sk_hl_click_bc()
            time.sleep(1)
            text = self.skHl.sk_hl_get_bcts()
            self.skHl.sk_hl_click_tsok()
            self.assertIn("金额有误", text)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 修改固定面值成功
    def test07_sk_hl(self):
        try:
            self.skHl.sk_hl_get_srs()[3].clear()
            self.skHl.sk_hl_get_srs()[3].send_keys("50")
            self.skHl.sk_hl_get_srs()[4].clear()
            self.skHl.sk_hl_get_srs()[4].send_keys("8.8")
            self.skHl.sk_hl_click_bc()
            time.sleep(1)
            text = self.skHl.sk_hl_get_bccg()
            self.assertEqual(text, "保存成功")
            time.sleep(1)
            self.skHl.sk_hl_click_bcok()
            time.sleep(1)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 增加固定面值100成功，并打开状态开关
    def test08_sk_hl(self):
        try:
            self.skHl.sk_hl_click_tj()
            time.sleep(1)
            self.skHl.sk_hl_click_je5()
            time.sleep(2)
            self.skHl.sk_hl_click_gdmz()
            self.skHl.sk_hl_input_fw5("100")
            self.skHl.sk_hl_input_hl5("8.8")
            # 状态打开，生效
            self.skHl.sk_hl_get_zts()[-1].click()
            self.skHl.sk_hl_click_bc()
            text = self.skHl.sk_hl_get_bccg()
            self.assertEqual(text, "保存成功")
            time.sleep(1)
            self.skHl.sk_hl_click_bcok()
            time.sleep(1)
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 删除卡种成功
    # def test08_sk_sc(self):
    #     # 点击删除
    #     self.skHl.sk_hl_click_sc()
    #     time.sleep(1)
    #     self.skHl.sk_hl_click_scqd()
    #     text = self.skHl.sk_hl_get_bccg()
    #     self.assertEqual(text, "保存成功")
    #     time.sleep(1)
    #     self.skHl.sk_hl_click_bcok()
